﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using AForge.Video.DirectShow;
using GalaSoft.MvvmLight.CommandWpf;
using System.Threading;
using System.Timers;
using System.Windows;
using AForge.Controls;

namespace AutoStar.ViewModel
{
    public class WindowMonitorVM : BaseViewModel
    {
        public WindowMonitorVM()
        {
            //FropVideos = new RelayCommand(DropVideo);
            StartRecord = new RelayCommand(StartRecordCommand);
            PauseRecord = new RelayCommand(PauseRecordCommand);
            EndRecord = new RelayCommand(EndRecordCommand);
            TakePicture = new RelayCommand(TakePictureCommand);
            OpenPictureFiles = new RelayCommand(OpenPictureFilesCommand);
            OpenVideoFiles = new RelayCommand(OpenVideoFilesCommand);
        }
        /// <summary>
        /// 初始化加载
        /// </summary>
        public void WindowMonitor_load()
        {
            VideoDeviceList = new ObservableCollection<string>();
            DropVideo();
            DropFrameRate();
            VideoSelectedValue = "请选择摄像头";

            timeCount.Interval = 1000;
            timeCount.AutoReset = true;
            timeCount.Elapsed += TimeCount_Elapsed;
        }

        private void DropFrameRate()
        {
            FrameRateList = new ObservableCollection<string>();
            FrameRateList.Add("正常");
            FrameRateList.Add("向左旋转90度");
            FrameRateList.Add("向右旋转90度");
        }

        private void StartRecordCommand()
        {
            //TODO: Start Record
        }

        private void PauseRecordCommand()
        {
            //TODO: Pause Record
        }

        private void EndRecordCommand()
        {
            //TODO: End Record
        }

        private void TakePictureCommand()
        {
            //TODO: Take Picture
        }

        private void OpenPictureFilesCommand()
        {
            //TODO: Open Picture Files
        }

        private void OpenVideoFilesCommand()
        {
            //TODO: Open Video Files
        }

        public void DropVideo()
        {
            VideoDeviceList.Clear();
            VideoDeviceList.Add("请选择摄像头");
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            foreach (FilterInfo device in videoDevices)
            {
                VideoDeviceList.Add(device.Name);
            }
        }
        public void SelectVideo()
        {
        }
        private void TimeCount_Elapsed(object sender, ElapsedEventArgs e)
        {
            tickNum++;
            int temp = tickNum;
            int sec = temp % 60;
            int min = temp / 60;
            if (60 == min)
            {
                min = 0;
                min++;
            }
            int hour = min / 60;
            String tick = "";
            if (hour < 10)
            {
                tick += "0" + hour.ToString();
            }
            else
            {
                tick += hour.ToString();
            }
            tick += ":";
            if (min < 10)
            {
                tick += "0" + min.ToString();
            }
            else
            {
                tick += min.ToString();
            }
            tick += ":";
            if (sec < 10)
            {
                tick += "0" + sec.ToString();
            }
            else
            {
                tick += sec.ToString();
            }
            Application.Current.Dispatcher.BeginInvoke(new Action(() =>
            {
                lblRECTime = tick;
            }));
        }
        private BitmapImage BitmapToBitmapImage(Bitmap bitmap)
        {
            if (bitmap == null)
                return null;

            using (var memory = new MemoryStream())
            {
                bitmap.Save(memory, ImageFormat.Png); // 或者其他你需要的格式  
                memory.Position = 0;
                var bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = memory;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();
                return bitmapImage;
            }
        }
        public void ShutCamera()
        {
            //if (videoSourcePlayer1.VideoSource != null)
            //{
            //    if (videoWriter.IsOpen)
            //    {
            //        videoWriter.Close();
            //        isRecord = false;
            //        timeCount.Stop();
            //        tickNum = 0;
            //        //lightIsREC.State = UILightState.Off;
            //        lblRECTime.Text = "00:00:00";
            //    }
            //    videoSourcePlayer1.SignalToStop();
            //    videoSourcePlayer1.WaitForStop();
            //    videoSourcePlayer1.VideoSource = null;
            //}
        }
        System.Timers.Timer timeCount = new System.Timers.Timer();
        VideoCaptureDevice videoSource;
        bool isRecord = false;
        int tickNum = 0;
        FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
        private ObservableCollection<string> _VideoDeviceList;
        public ObservableCollection<string> VideoDeviceList
        {
            get => _VideoDeviceList;
            set => Set(ref _VideoDeviceList, value);
        }
        private ObservableCollection<string> _FrameRateList;
        public ObservableCollection<string> FrameRateList
        {
            get => _FrameRateList;
            set => Set(ref _FrameRateList, value);
        }
        public ICommand WindowMonitor_Loaded { get; private set; }
        public ICommand FropVideos { get; private set; }
        public ICommand StartRecord { get; private set; }
        public ICommand PauseRecord { get; private set; }
        public ICommand EndRecord { get; private set; }
        public ICommand TakePicture { get; private set; }
        public ICommand OpenPictureFiles { get; private set; }
        public ICommand OpenVideoFiles { get; private set; }

        private BitmapImage _ImageSource;
        public BitmapImage ImageSource
        {
            get => _ImageSource;
            set => Set(ref _ImageSource, value);
        }
        private string _VideoSelectedValue;
        public string VideoSelectedValue
        {
            get => _VideoSelectedValue;
            set
            {
                Set(ref _VideoSelectedValue, value);
                ShutCamera();
                //videoSource = new VideoCaptureDevice(videoDevices[cbbVideoDevice.SelectedIndex].MonikerString);
                //videoSourcePlayer1.VideoSource = videoSource;
                //videoSourcePlayer1.NewFrame += VideoSourcePlayer_NewFrame;
                //videoSourcePlayer1.Start();
            }
        }
        private int _VideoSelectedIndex;
        public int VideoSelectedIndex
        {
            get => _VideoSelectedIndex;
            set => Set(ref _VideoSelectedIndex, value);
        }
        private string _lblRECTime;
        public string lblRECTime
        {
            get => _lblRECTime;
            set => Set(ref _lblRECTime, value);
        }
        private int _FrameRateSelectedIndex;
        public int FrameRateSelectedIndex
        {
            get => _FrameRateSelectedIndex;
            set => Set(ref _FrameRateSelectedIndex, value);
        }
        private string _lblFocus;
        public string lblFocus
        {
            get => _lblFocus;
            set
            {
                Set(ref _lblFocus, value);
                //Global.Monitor.SetVideoParas(1, value);
            }
        }
    }
}
